<?php
namespace Friend\Controller;
use Common\Controller\CommonController;

/**
 * 好友联系人模块
 */
class FriendController extends CommonController {
	protected $_friend_report;
	
	protected function _initialize(){
		parent::_initialize();
		$this->_mycache = S(C('REDIS_DSN'));
		$this->_auth();
		
		$this->_moduleid = 102;
		$this->_friend = new \Common\Model\FriendModel();
		$this->_friend_report = new \Friend\Model\FriendReportModel();
	}
    
	/**
	 * 获取好友列表
	 */
    public function getFriendListAction(){
		$this->_getMyFriendList();
    }
    
    private function _getMyFriendList(){
    	$friendList = $this->_getFriendList($this->_custInfo['custid']);
    	 
    	if(!empty($friendList)){
    		$this->data = $friendList;
    	}
    	
    	$this->outPut();
    }
    
    
    /**
     * 请求添加好友
     */
    public function addFriendAction(){
    	$custid = (int)I('request.custid');
    	$notes = I('request.notes');
    	$this->_addFriend($custid,$notes);
    }
    
    private function _addFriend($custid,$notes){
    	if(empty($custid)){
    		$this->status = 1000;
    		$this->msg = L('STATUS_1000');
    		$this->outPut();
    	}
    	
    	if($custid == $this->_custInfo['custid']){
    		$this->status = 1001;
    		$this->msg = L('STATUS_1001');
    		$this->outPut();
    	}
    	
    	$data['custid'] = $this->_custInfo['custid'];
    	$data['tcustid'] = $custid;
    	$ret = $this->_friend->getFriend($data);
    	if(!empty($ret)){
    		$this->outPut();
    	}
    	 
    	$custInfo = $this->_getCustInfo($custid);
    	if(empty($custInfo)){
    		$this->status = 1001;
    		$this->msg = L('STATUS_1001');
    		$this->outPut();
    	}
    	
    	$applyKey = \cachekey::$key['BASE_KEY_CONF']['APPLY_FRIEND_TIME'].$this->_custInfo['custid'].":".$custid;
    	$timelast = $this->_mycache->get($applyKey);
    	
    	if(__ENV__ == "production"){
	    	if(time() - (int)$timelast > (int)C('DENY_APPLY_FRIEND_EXPIRE')){
	    		$this->_mycache->set($applyKey , time());
	    		$this->_mycache->expire($applyKey , C('DENY_APPLY_FRIEND_EXPIRE'));
	    		
	    		//发推送消息
	    		$content = empty($notes) ? L('ADD_FRIEND_NOTICE' , array('nickname'=>$this->_custInfo['nickname'])) : $notes;
	    		$this->_addFriendNotice($custid , $this->_custInfo , $content);
	    	}
    	}else{
    		//发推送消息
    		$content = empty($notes) ? L('ADD_FRIEND_NOTICE' , array('nickname'=>$this->_custInfo['nickname'])) : $notes;
    		$this->_addFriendNotice($custid , $this->_custInfo , $content);
    	}
    	
    	$this->outPut();
    }
    
    
    private function _addFriendNotice($tcustid , $custInfo , $content){
    	$noticeData = array();
    	$noticeData['content'] = $content;
    	$noticeData['custid'] = $custInfo['custid'];
    	$noticeData['tcustid'] = $tcustid;
    	$noticeData['moduleid'] = $this->_moduleid;
    	$noticeid = $this->_createNotice($noticeData);
    	
    	$msg['from_cust_name'] = $custInfo['nickname'];
    	$msg['from_cust_id'] = (int)$custInfo['custid'];
    	$msg['to_cust_id'] = (int)$tcustid;
    	$msg['type'] = 8;
    	$msg['content'] = $content;
    	$msg['notice_id'] = (string)$noticeid;
    	\im::sendMsg(3, array($tcustid), array(), $msg);
    }
    
    
    /**
     * 确认添加好友
     */
    public function acceptFriendAction(){
    	$noticeid = (int)I('request.noticeid');
    	$accept = (int)I('request.accept');
    	$this->_acceptFriend($noticeid , $accept);
    }
    
    private function _acceptFriend($noticeid , $accept = 1){
    	if(empty($noticeid)){
    		$this->status = 1000;
    		$this->msg = L('STATUS_1000');
    		$this->outPut();
    	}
    	
    	//check notice
    	$noticeInfo = $this->_getNotice(array('noticeid'=>$noticeid));
    	if(!empty($noticeInfo)){
    		if($this->_custInfo['custid'] != $noticeInfo['tcustid']){
    			$this->status = 1001;
    			$this->msg = L('STATUS_1001');
    			$this->outPut();
    		}
    		$custid = $noticeInfo['custid'];
    	}else{
    		$this->status = 1001;
    		$this->msg = L('STATUS_1001');
    		$this->outPut();
    	}

    	if(empty($accept)){//拒绝
    		$this->outPut();
    	}

    		
		$this->_friend->startTrans();
		$data = array();
		$data['custid'] = $custid;
		$data['tcustid'] = $this->_custInfo['custid'];
		//$data['nickname'] = $this->_custInfo['nickname'];
		$data['status'] = 1;
		$ret1 = $this->_friend->addFriend($data);//对方将自己加为好友
		
		
		$data = array();
		$data['custid'] = $this->_custInfo['custid'];
		$data['tcustid'] = $custid;
		$custInfo = $this->_getCustInfo($custid);
		//$data['nickname'] = $custInfo['nickname'];
		$data['status'] = 1;
		$ret2 = $this->_friend->addFriend($data);//将对方加为自己的好友
		if($ret1 && $ret2){
			$this->_friend->commit();
			$this->_delFriendListCache($custid);//删除缓存信息
			$this->_delFriendListCache($this->_custInfo['custid']);//删除缓存信息
			
			//发推送消息
			$content = L('ACCEPT_FRIEND_NOTICE' , array('nickname'=>$this->_custInfo['nickname']));
			$this->_acceptFriendNotice($custid , $this->_custInfo , $noticeid , $content);
			
			$custInfo = $this->_getCustInfo($custid);
			$this->data = $custInfo;
			$this->outPut();
		}else{
			$this->_friend->rollback();
			$this->status = 1204;
			$this->msg = L('STATUS_1204');
			$this->outPut();
		}
    }
    
    
    private function _acceptFriendNotice($tcustid , $custInfo , $noticeid , $content){
    	$noticeData = array();
    	$noticeData['content'] = $content;
    	$noticeData['custid'] = $custInfo['custid'];
    	$noticeData['tcustid'] = $tcustid;
    	$noticeData['returnid'] = $noticeid;
    	$noticeData['moduleid'] = $this->_moduleid;
    	$noticeData['status'] = 2;
    	$this->_createNotice($noticeData);
    	$this->_updateNotice(array('noticeid'=>$noticeid , 'tcustid'=>$custInfo['custid'] , 'status'=>2));
    	\im::addFriend($custInfo['custid'] , $tcustid);
    	\im::addFriend($tcustid , $custInfo['custid']);
    	
    	$msg['from_cust_name'] = $custInfo['nickname'];
    	$msg['from_cust_id'] = (int)$custInfo['custid'];
    	$msg['to_cust_id'] = (int)$tcustid;
    	$msg['type'] = 10;
    	//$msg['session_type'] = 1;
    	//$msg['session_id'] = (int)$custInfo['custid'];
    	$msg['content'] = $content;
    	$msg['notice_id'] = (string)$noticeid;
    	$msg['data'] = $custInfo;
    	\im::sendMsg(3, array($tcustid), array(), $msg);
    }
    
    
    /**
     * 删除好友
     */
    public function deleteFriendAction(){
    	$custid = (int)I('request.custid');
    	$this->_deleteFriend($custid);
    }
    
    private function _deleteFriend($custid){
    	if(empty($custid) || $custid == C('WELCOME_CUSTID')){
    		$this->status = 1000;
    		$this->msg = L('STATUS_1000');
    		$this->outPut();
    	}
    	
    	
    	$this->_friend->startTrans();
    	$data = array();
    	$data['custid'] = $custid;
    	$data['tcustid'] = $this->_custInfo['custid'];
    	$ret1 = $this->_friend->deleteFriend($data);
    	$data = array();
    	$data['custid'] = $this->_custInfo['custid'];
    	$data['tcustid'] = $custid;
    	$ret2 = $this->_friend->deleteFriend($data);
    	if($ret1 && $ret2){
    		$this->_friend->commit();
    		$this->_delFriendListCache($custid);
    		$this->_delFriendListCache($this->_custInfo['custid']);
    		
    		$content = L('DEL_FRIEND_NOTICE');
    		$this->_delFriendNotice($custid , $this->_custInfo , $content);
    		
    		$this->outPut();
    	}else{
    		$this->_friend->rollback();
    		$this->status = 1207;
    		$this->msg = L('STATUS_1207');
    		$this->outPut();
    	}
    }
    
    
    private function _delFriendNotice($tcustid , $custInfo , $content){
    	$msg['from_cust_name'] = $custInfo['nickname'];
    	$msg['from_cust_id'] = (int)$custInfo['custid'];
    	$msg['to_cust_id'] = (int)$tcustid;
    	$msg['type'] = 30;
    	$msg['content'] = $content;
    	\im::sendMsg(3, array($tcustid), array(), $msg);
    	
    	\im::delFriend($custInfo['custid'] , $tcustid);
    	\im::delFriend($tcustid , $custInfo['custid']);
    }
    
    
    /**
     * 举报好友
     */
    public function friendReportAction(){
    	$custid = (int)I('request.custid');
    	$content = I('request.content');
    	$spamtype = (int)I('request.spamtype');
    	$this->_friendReport($custid , $content , $spamtype);
    }
    
    private function _friendReport($custid , $content , $spamtype){
    	if(empty($custid) || empty($content)){
    		$this->status = 1000;
    		$this->msg = L('STATUS_1000');
    		$this->outPut();
    	}
    	
    	$data = array();
    	$data['custid'] = $this->_custInfo['custid'];
    	$data['tcustid'] = $custid;
    	$data['content'] = $content;
    	$data['spamtype'] = $spamtype;
    	
    	$result = $this->_friend_report->getSpam($data);
    	if(!empty($result)){
    		$this->status = 1206;
    		$this->msg = L('STATUS_1206');
    		$this->outPut();
    	}
    	
    	$ret = $this->_friend_report->addSpam($data);
    	if($ret){
    		$this->outPut();
    	}else{
    		$this->status = 1205;
    		$this->msg = L('STATUS_1205');
    		$this->outPut();
    	}
    }
    
    
    
    /**
     * 消息设置
     */
    public function msgSettingAction(){
    	$custid = (int)I('request.custid');
    	$beep = (int)I('request.beep');
    	$push = (int)I('request.push');
    
    	$this->_msgSetting($custid , $beep , $push);
    }
    
    
    private function _msgSetting($custid , $push){
    	if(empty($custid)){
    		$this->status = 1000;
    		$this->msg = L('STATUS_1000');
    		$this->outPut();
    	}
    	
    	$data = array();
    	$data['tcustid'] = $custid;
    	$data['custid'] = $this->_custInfo['custid'];
    	$setting = array('push'=>(string)$push);
    	$data['msgsetting'] = json_encode($setting);
    	$ret = $this->_friend->updateFriend($data);
    	if($ret){
    		$this->_delFriendListCache($this->_custInfo['custid']);//删除缓存信息
    		
    		if($push <= 0)
    			$this->_mycache->srem(\cachekey::$key['BASE_KEY_CONF']['NO_PUSH_CUST'].$custid, $this->_custInfo['custid']);
    		else
    			$this->_mycache->sadd(\cachekey::$key['BASE_KEY_CONF']['NO_PUSH_CUST'].$custid, $this->_custInfo['custid']);
    
    		$this->outPut();
    	}else{
    		$this->status = 1209;
    		$this->msg = L('STATUS_1209');
    		$this->outPut();
    	}
    }
    
    
    /**
     * 设置好友备注
     */
    public function setFriendMemoAction(){
    	$custid = (int)I('request.custid');
    	$nickname = I('request.nickname');
    	$phonenumber = I('request.phonenumber');
    	$tagids = I('request.tagids');
    	$memo = I('request.memo');
    	$this->_setFriendMemo($custid , $nickname , $phonenumber , $memo , $tagids);
    }
    
    private function _setFriendMemo($custid , $nickname , $phonenumber , $memo , $tagids=null){
    	if(empty($custid)){
    		$this->status = 1000;
    		$this->msg = L('STATUS_1000');
    		$this->outPut();
    	}
    	
    	$data['custid'] = $this->_custInfo['custid'];
    	$data['tcustid'] = $custid;
    	$data['nickname'] = $nickname;
    	$data['phonenumber'] = $phonenumber;
    	$data['memo'] = $memo;
    	$ret = $this->_friend->updateFriend($data);
    	if($ret){
    		if(!empty($tagids)){
    		$tagids = explode(",", $tagids);
	    		if(!empty($tagids)){
	    			foreach($tagids as $tagid){
	    				//添加标签成员
						$this->_mycache->sadd(\cachekey::$key['BASE_KEY_CONF']['FRIENDS_TAG_CUST'] . $tagid , $custid);
	    			}
	    		}
    		}
    		
    		$this->_delFriendListCache($this->_custInfo['custid']);
    	}
    	
    	$this->outPut();
    }
    
    
    /**
     * 获取好友备注
     */
    public function getFriendMemoAction(){
    	$custid = (int)I('request.custid');
    	$this->_getFriendMemo($custid);
    }
    
    
    private function _getFriendMemo($custid){
		if(empty($custid)){
    		$this->status = 1000;
    		$this->msg = L('STATUS_1000');
    		$this->outPut();
    	}
    	
    	$data['custid'] = $this->_custInfo['custid'];
    	$data['tcustid'] = $custid;
    	$friendInfo = $this->_friend->getFriend($data);
    	$friendInfo['tags'] = "";
    	if(!empty($friendInfo)){
    		$friendInfo['msgsetting'] = json_decode($friendInfo['msgsetting'] , true);
    		$allTagIds = $this->_mycache->smembers(\cachekey::$key['BASE_KEY_CONF']['FRIENDS_TAGS'] . $this->_custInfo['custid']);
    		if(!empty($allTagIds)){
    			foreach($allTagIds as $tagid){
    				$ret = $this->_mycache->sismember(\cachekey::$key['BASE_KEY_CONF']['FRIENDS_TAG_CUST'] . $tagid , $custid);
    				if($ret){
    					$friendInfo['tags'][] = $tagid;
    				}
    			}
    		}
    	}
    	
    	$this->data = $friendInfo;
    	$this->outPut();
    }
    
    
    
    
    /**
     * 获取个人标签列表
     */
    public function getTagsAction(){
    	$this->_getTags();
    }
    
    private function _getTags(){
    	$returnData = array();
		//我的标签
    	$tagids = $this->_mycache->smembers(\cachekey::$key['BASE_KEY_CONF']['FRIENDS_TAGS'] . $this->_custInfo['custid']);
    	if(!empty($tagids)){
	    	foreach($tagids as $idx => $tagid){
	    		$tagname = $this->_mycache->get(\cachekey::$key['BASE_KEY_CONF']['FRIENDS_TAG'] . $tagid);
	    		$custids = $this->_mycache->smembers(\cachekey::$key['BASE_KEY_CONF']['FRIENDS_TAG_CUST'] . $tagid);
	    		
	    		$returnData[$idx]['tagid'] = (string)$tagid;
	    		$returnData[$idx]['tagname'] = (string)$tagname;
	    		if(!empty($custids)){
	    			$returnData[$idx]['custnum'] = (string)count($custids);
	    			foreach($custids as $k=>$custid){
	    				$custInfo = $this->_getCustInfo($custid);
	    				$returnData[$idx]['custs'][$k]['custid'] = (string)$custid;
	    				$returnData[$idx]['custs'][$k]['nickname'] = $custInfo['nickname'];
	    			}
	    		}else{
	    			$returnData[$idx]['custnum'] = "0";
	    			$returnData[$idx]['custs'] = array();
	    		}
	    	}
    	}
    	
    	$this->data = $returnData;
    	$this->outPut();
    }
    
    
    /**
     * 创建标签
     */
    public function createTagAction(){
    	$tagname = I('request.tagname');
    	$custids = I('request.custids');
    
    	$this->_createTag($tagname , $custids);
    }
    
    private function _createTag($tagname , $custids = ""){
        if(empty($tagname)){
    		$this->status = 1000;
    		$this->msg = L('STATUS_1000');
    		$this->outPut();
    	}
    	
    	$friendTagIdKey = \cachekey::$key['BASE_KEY_CONF']['FRIENDS_TAG_ID'];
    	$tagid = $this->_mycache->incr($friendTagIdKey);
    	
    	$returnData = array();
    	
    	//在我的标签中添加
    	$this->_mycache->sadd(\cachekey::$key['BASE_KEY_CONF']['FRIENDS_TAGS'] . $this->_custInfo['custid'] , $tagid);
    	//创建标签
    	$this->_mycache->set(\cachekey::$key['BASE_KEY_CONF']['FRIENDS_TAG'] . $tagid , $tagname);
    	
    	if(!empty($custids)) $custids = explode(",", $custids);
    	if(!empty($custids)){
    		$returnData['custnum'] = count($custids);
    		$returnData['custnum'] = (string)$returnData['custnum'];
	    	foreach($custids as $k=>$custid){
	    		//添加标签成员
	    		$this->_mycache->sadd(\cachekey::$key['BASE_KEY_CONF']['FRIENDS_TAG_CUST'] . $tagid , $custid);
	    		
	    		$custInfo = $this->_getCustInfo($custid);
	    		$returnData['custs'][$k]['custid'] = $custid;
	    		$returnData['custs'][$k]['nickname'] = $custInfo['nickname'];
	    	}
    	}else{
    		$returnData['custnum'] = "0";
    		$returnData['custs'] = array();
    	}
    	
    	$returnData['tagid'] = (string)$tagid;
    	$returnData['tagname'] = $tagname;
    	
    	$this->data = $returnData;
    	$this->outPut();
    }
    
    
    /**
     * 修改标签
     */
    public function updateTagAction(){
    	$tagid = (int)I('request.tagid');
    	$tagname = I('request.tagname');
    	$custids = I('request.custids');
    	$this->_updateTag($tagid , $tagname , $custids);
    }
    
    private function _updateTag($tagid , $tagname , $custids = null){
    	if(empty($tagid) || empty($tagname)){
    		$this->status = 1000;
    		$this->msg = L('STATUS_1000');
    		$this->outPut();
    	}
    	
        $ret = $this->_mycache->sismember(\cachekey::$key['BASE_KEY_CONF']['FRIENDS_TAGS'] . $this->_custInfo['custid'] , $tagid);
    	if(!$ret){
    		$this->status = 1001;
    		$this->msg = L('STATUS_1001');
    		$this->outPut();
    	}
    	
    	$returnData = array();
    	
    	//修改标签名
    	$this->_mycache->set(\cachekey::$key['BASE_KEY_CONF']['FRIENDS_TAG'] . $tagid , $tagname);
    	
    	//删除标签中的成员
    	$this->_mycache->del(\cachekey::$key['BASE_KEY_CONF']['FRIENDS_TAG_CUST'] . $tagid);
    	if(!empty($custids)) $custids = explode(",", $custids);
    	if(!empty($custids)){
    		$returnData['custnum'] = (string)count($custids);
    		foreach($custids as $k=>$custid){
    			//添加标签成员
    			$this->_mycache->sadd(\cachekey::$key['BASE_KEY_CONF']['FRIENDS_TAG_CUST'] . $tagid , $custid);
    			$custInfo = $this->_getCustInfo($custid);
    			$returnData['custs'][$k]['custid'] = $custid;
    			$returnData['custs'][$k]['nickname'] = $custInfo['nickname'];
    		}
    	}else{
    		$returnData['custnum'] = "0";
    		$returnData['custs'] = array();
    	}
    	
    	$returnData['tagid'] = $tagid;
    	$returnData['tagname'] = $tagname;
    	
    	$this->data = $returnData;
    	$this->outPut();
    }
    
    
    /**
     * 删除标签
     */
    public function deleteTagAction(){
    	$tagid = (int)I('request.tagid');
    	$this->_deleteTag($tagid);
    }
    
    private function _deleteTag($tagid){
		if(empty($tagid)){
    		$this->status = 1000;
    		$this->msg = L('STATUS_1000');
    		$this->outPut();
    	}
    	
    	$ret = $this->_mycache->sismember(\cachekey::$key['BASE_KEY_CONF']['FRIENDS_TAGS'] . $this->_custInfo['custid'] , $tagid);
    	if(!$ret){
    		$this->status = 1001;
    		$this->msg = L('STATUS_1001');
    		$this->outPut();
    	}
    	
    	//从我的标签中删除
    	$this->_mycache->srem(\cachekey::$key['BASE_KEY_CONF']['FRIENDS_TAGS'] . $this->_custInfo['custid'] , $tagid);
    	//删除标签
    	$this->_mycache->rm(\cachekey::$key['BASE_KEY_CONF']['FRIENDS_TAG'] . $tagid);
    	//删除标签中的成员
    	$this->_mycache->del(\cachekey::$key['BASE_KEY_CONF']['FRIENDS_TAG_CUST'] . $tagid);  

    	$this->outPut();
    }
    
    
    
    /**
     * 根据手机号判断是否好友
     */
    public function checkByMobileAction(){
    	$mobiles = I('request.mobiles');
    	$this->_checkByMobile($mobiles);
    }
    
    private function _checkByMobile($mobiles){
    	if(empty($mobiles)){
    		$this->status = 1000;
    		$this->msg = L('STATUS_1000');
    		$this->outPut();
    	}
    	
    	$friends = array();
    	$friendList = $this->_getFriendList($this->_custInfo['custid']);
    	if(!empty($friendList)){
    		foreach($friendList as $friend){
    			$friends[$friend['custid']] = 1;
    		}
    	}
    	
    	$returnData = array();
    	$mobiles = explode(",", $mobiles);
    	foreach($mobiles as $idx=>$mobile){
	    	$mobileKey = \cachekey::$key['BASE_KEY_CONF']['CUST_MOBILE'].$mobile;
	    	$custid = (string)$this->_mycache->get($mobileKey);
	    	
	    	$custInvitKey = \cachekey::$key['BASE_KEY_CONF']['CUST_REGISTER_INVIT'].$this->_custInfo['custid'].":".$mobile;
	    	$invited = $this->_mycache->get($custInvitKey);
	    	if(empty($invited)){
	    		$returnData[$idx]['_invited'] = "0";
	    	}else{
	    		$returnData[$idx]['_invited'] = "1";
	    	}
	    	
	    	$returnData[$idx]['mobile'] = $mobile;
	    	$returnData[$idx]['custid'] = empty($custid) ? "" : $custid;
	    	if(empty($custid)){
	    		$returnData[$idx]['nickname'] = "";
	    		$returnData[$idx]['_isfriend'] = "0";
	    	}else{
	    		$custInfo = $this->_getCustInfo($custid);
	    		$returnData[$idx]['nickname'] = $custInfo['nickname'];
	    		$returnData[$idx]['_isfriend'] = empty($friends[$custid]) ? "0" : "1";
	    	}
    	}
    	
    	
    	$this->data = $returnData;
    	$this->outPut();
    }
}
